//
//  AI.m
//  
//
//  Created by Joan Bartrina Rapesta on 10/4/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ConditionGame.h"
#import "Player.h"
#import "Board.h"

#define NUM_PLAYERS_MAX 10

@implementation CConditionGame

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
        
    }
    
    return self;
}

- (void) run{
    NSLog(@"CONDITION GAME ESTEM AL RUN!!!!");
    for(int i = 0; i < m_uiNumplayers; i++)
    {
        if(m_Players[i]->m_HandValue.fold == true){
            m_Players[i]->m_HandValue.hand_value_primary = 11;
            m_Players[i]->m_HandValue.win_hand = -1;

        }
    }
    
    for(int i = 0; i < m_uiNumplayers; i++){
        NSLog(@"INFO DE LA MILLOR JUGADA PEL PLAYER %i",i);
        NSLog(@"m_HandValue.fold: %i",m_Players[i]->m_HandValue.fold);
        NSLog(@"m_HandValue.hand_value_primary: %i",m_Players[i]->m_HandValue.hand_value_primary);
        NSLog(@"m_HandValue.hand_value_secondary: %i",m_Players[i]->m_HandValue.hand_value_secondary);
        NSLog(@"m_HandValue.hand_value_third: %i",m_Players[i]->m_HandValue.hand_value_third);
        NSLog(@"m_HandValue.hand_value_fourth: %i",m_Players[i]->m_HandValue.hand_value_fourth);
        NSLog(@"m_HandValue.hand_value_fith: %i",m_Players[i]->m_HandValue.hand_value_fith);
        NSLog(@"m_HandValue.flush_figure: %i",m_Players[i]->m_HandValue.flush_figure);
        NSLog(@"m_HandValue.left: %i",m_Players[i]->m_HandValue.left);
        NSLog(@"m_HandValue.left: %i",m_Players[i]->m_HandValue.left);
        NSLog(@"m_HandValue.win_hand: %i",m_Players[i]->m_HandValue.win_hand);
    }
    
    //primer mirem si hi ha algu que guanya pel hand_value_primary si mes d'un te el mateix valor de hand_value_primary posem el valor de 
 
    bool drawPlayers[m_uiNumplayers];
    unsigned int draw = 1;
    int tmpwinner = -1;
  
    for(int i = 0; i < m_uiNumplayers; i++){
        if(m_Players[i]->m_HandValue.win_hand != -1){
            if(tmpwinner == -1){
                tmpwinner = i;
            }else{
                if(m_Players[i]->m_HandValue.hand_value_primary < m_Players[tmpwinner]->m_HandValue.hand_value_primary){
                    for(int ii = 0; ii < i; ii++){
                        if(m_Players[ii]->m_HandValue.win_hand != -1){
                            m_Players[ii]->m_HandValue.win_hand = 0;
                            drawPlayers[ii] = false;
                        }
                    }
                    m_Players[i]->m_HandValue.win_hand = 1;
                    draw = 1;
                    tmpwinner = i;
                }else{
                    if(m_Players[i]->m_HandValue.hand_value_primary == m_Players[tmpwinner]->m_HandValue.hand_value_primary){
                        //mirar el hand_value_second
                        if(m_Players[i]->m_HandValue.hand_value_secondary != m_Players[tmpwinner]->m_HandValue.hand_value_secondary){
                            if(m_Players[i]->m_HandValue.hand_value_secondary > m_Players[tmpwinner]->m_HandValue.hand_value_secondary){
                                for(int ii = 0; ii < i; ii++){
                                    if(m_Players[ii]->m_HandValue.win_hand != -1){
                                        m_Players[ii]->m_HandValue.win_hand = 0;
                                        drawPlayers[ii] = false;
                                    }
                                }
                                m_Players[i]->m_HandValue.win_hand = 1;
                                draw = 1;
                                tmpwinner = i;
                            }
                        }else{
                            if(m_Players[i]->m_HandValue.hand_value_third != m_Players[tmpwinner]->m_HandValue.hand_value_third){
                                if(m_Players[i]->m_HandValue.hand_value_third > m_Players[tmpwinner]->m_HandValue.hand_value_third){
                                    for(int ii = 0; ii < i; ii++){
                                        if(m_Players[ii]->m_HandValue.win_hand != -1){
                                            m_Players[ii]->m_HandValue.win_hand = 0;
                                            drawPlayers[ii] = false;
                                        }
                                    }
                                    m_Players[i]->m_HandValue.win_hand = 1;
                                    draw = 1;
                                    tmpwinner = i;
                                }
                            }else{
                                if(m_Players[i]->m_HandValue.hand_value_fourth != m_Players[tmpwinner]->m_HandValue.hand_value_fourth){
                                    if(m_Players[i]->m_HandValue.hand_value_fourth > m_Players[tmpwinner]->m_HandValue.hand_value_fourth){
                                        for(int ii = 0; ii < i; ii++){
                                            if(m_Players[ii]->m_HandValue.win_hand != -1){
                                                m_Players[ii]->m_HandValue.win_hand = 0;
                                                drawPlayers[ii] = false;
                                            }
                                        }
                                        m_Players[i]->m_HandValue.win_hand = 1;
                                        draw = 1;
                                        tmpwinner = i;
                                    }
                                }else{
                                    if(m_Players[i]->m_HandValue.hand_value_fith != m_Players[tmpwinner]->m_HandValue.hand_value_fith){
                                        if(m_Players[i]->m_HandValue.hand_value_fith > m_Players[tmpwinner]->m_HandValue.hand_value_fith){
                                            for(int ii = 0; ii < i; ii++){
                                                if(m_Players[ii]->m_HandValue.win_hand != -1){
                                                    m_Players[ii]->m_HandValue.win_hand = 0;
                                                    drawPlayers[ii] = false;
                                                }
                                            }
                                            m_Players[i]->m_HandValue.win_hand = 1;
                                            draw = 1;
                                            tmpwinner = i;
                                        }
                                    }else{
                                        draw++;
                                        drawPlayers[i] = true;
                                        drawPlayers[tmpwinner] = true;
                                    }
                                } 
                            }
                        }
                    }
                }
            }
        }
    }
    
    for(int i = 0; i < m_uiNumplayers; i++)
    {
        if(drawPlayers[i] == true){
            m_Players[i]->m_HandValue.win_hand = 1;
        }else{
            m_Players[i]->m_HandValue.win_hand = 0;
        }
    }
    
    if(draw == 1){
        NSLog(@"UN SOL JUGADOR HA GUANYAT");
        //el guanyador mirar el total bet de cada jugador i es queda la quantitat de cada jugador per la que el guanyador a apostat
         for(int i = 0; i < m_uiNumplayers; i++){
             if(m_Players[i]->m_HandValue.win_hand == 1){
                 if ([m_Players[i] getPlayerState] == PlayerState_AllIn){
                     //ALL IN guanyar pel que has apostat i retornar pasta als altres players
                     for(int ii = 0; ii < m_uiNumplayers; ii++){
                         if(m_Players[i]->m_uiTotalBet < m_Players[ii]->m_uiTotalBet){
                             m_Players[ii]->m_uiMoney = m_Players[ii]->m_uiMoney + (m_Players[ii]->m_uiTotalBet - m_Players[i]->m_uiTotalBet);
                             m_Players[ii]->m_uiTotalBet = m_Players[i]->m_uiTotalBet;
                         }else{
                             m_Players[i]->m_uiGain =  m_Players[i]->m_uiGain + m_Players[ii]->m_uiTotalBet;
                         }
                     }
                     m_Players[i]->m_uiMoney = m_Players[i]->m_uiMoney + m_Players[i]->m_uiGain;
                 }else{
                     for(int ii = 0; ii < m_uiNumplayers; ii++){
                         m_Players[i]->m_uiGain =  m_Players[i]->m_uiGain + m_Players[ii]->m_uiTotalBet;
                     }
                     m_Players[i]->m_uiMoney = m_Players[i]->m_uiMoney + m_Players[i]->m_uiGain;  
                 } 
             }             
         }
    }
    
    if(draw != 1){
        NSLog(@"ALGUNS JUGADORS HAN EMPATAT");
        int gain = m_uiBank / draw;
        int odd = m_uiBank % 2;
        bool AllPlayersSamebet = false;
        int lastbet;
        //mirar si tots els guanyadors han apostat per igual. Nomes potser diferent si s'ha fet un Allin
        lastbet = m_Players[0]->m_uiTotalBet;
        for(int i = 1; i < m_uiNumplayers; i++){
            if(lastbet == m_Players[i]->m_uiTotalBet){
                AllPlayersSamebet = true;
            }
        }
        
        //si tots fan la mateixa aposta nomes cal repartir entre els guanyadors el pot total.
        if(AllPlayersSamebet == true){
            for(int i = 0; i < m_uiNumplayers; i++){
                if(m_Players[i]->m_HandValue.win_hand == 1){
                    m_Players[i]->m_uiGain =  gain;
                    m_Players[i]->m_uiMoney = m_Players[i]->m_uiMoney + gain;
                }
            }
        }else{
            
            //no va
            for(int i = 0; i < m_uiNumplayers; i++){
                if(drawPlayers[i] == true){
                    m_Players[i]->m_HandValue.win_hand = 1;
                    if ([m_Players[i] getPlayerState] == PlayerState_AllIn){
                        
                    }else{
                        m_Players[i]->m_uiGain = gain + odd;//el odd s'ha de posar a l'equerra del big blind,ara no es correcte!!!!!!!
                        m_Players[i]->m_uiMoney = m_Players[i]->m_uiMoney + m_Players[i]->m_uiGain;
                        if(odd == 1){
                            odd = 0;
                        }
                    }
                }else{
                    m_Players[i]->m_HandValue.win_hand = 0;
                }
            }
 
        }
            
                
    }
    
    
    for(int i = 0; i < m_uiNumplayers; i++){
        if(m_Players[i]->m_HandValue.win_hand != 1){
            m_Players[i]->m_uiGain = m_Players[i]->m_uiTotalBet * -1;
        }
    }
    
    NSLog(@"==========Log ConditionGame================");
    for(int i = 0; i < m_uiNumplayers; i++){
        NSLog(@"draw: %i", draw);
        NSLog(@"Player %i m_HandValue.win_hand: %i",i, m_Players[i]->m_HandValue.win_hand);
        NSLog(@"Player %i m_Players.m_uiTotalBet: %i",i, m_Players[i]->m_uiTotalBet);
        //NSLog(@"Player %i m_Players.m_uiCurrentBet: %i",i, m_Players[i]->m_uiCurrentBet);
        //NSLog(@"Player %i m_Players.m_uiToBank: %i",i, m_Players[i]->m_uiToBank);
        //NSLog(@"Player %i m_Players.m_uiCall: %i",i, m_Players[i]->m_uiCall);
        //NSLog(@"Player %i m_Players.m_uiRaise: %i",i, m_Players[i]->m_uiRaise);
    }
    NSLog(@"BANK: %i", m_uiBank);
    NSLog(@"==========================");
    
}

- (void) setNumPlayers: (unsigned int) Numplayers{
    m_uiNumplayers = Numplayers;
}

- (void) setmPlayers: (CPlayer*) Player index:(int) i{
    m_Players[i] = Player;
}

- (void) setBank: (unsigned int) Bank{
    m_uiBank = Bank;
}

- (void) dealloc
{
	// don't forget to call "super dealloc"
	[super dealloc];
}

@end
